/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package taglibs;

import daos.ItemDAO;
import java.util.ArrayList;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import models.Item;
import models.ShoppingCart;
import util.Common;

/**
 *
 * @author trietto
 */
public class ShoppingCartGrid extends TagSupport {

    /**
     *
     */
    private static final long serialVersionUID = 1L;

    /*
     * (non-Javadoc)
     * @see javax.servlet.jsp.tagext.TagSupport#doStartTag()
     */
    public int doStartTag() {
        try {
            JspWriter writer = pageContext.getOut();
            ArrayList<ShoppingCart> lstCarts = (ArrayList<ShoppingCart>)(pageContext.getSession().getAttribute("carts"));
            if(lstCarts==null){
                lstCarts = new ArrayList<ShoppingCart>();
            }

            writer.println("<form action='CheckOut.jsp' method='POST'>");
            writer.println("<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");
            writer.println("<tr>");
            writer.println("<td colspan='6' align='center'><b>SHOPPING CART</b><br></td>");
            writer.println("</tr>");
            Item item = null;
            for (ShoppingCart shoppingCart : lstCarts) {
                item = ItemDAO.singleton().select(shoppingCart.getItemId());
                writer.println("<tr>");
                writer.println("<td>Book:</td>");
                writer.println("<td>"+item.getName()+"</td>");
                writer.println("<td>Quantity:</td>");
                writer.println("<td>"+shoppingCart.getQuantity()+"</td>");
                writer.println("<td>Price:</td>");
                writer.println("<td>"+shoppingCart.getPrice()+"</td>");
                writer.println("</tr>");
            }
            
            writer.println("<tr colspan='6'>");
            writer.println("<td>" + Common.createSubmitButton("btnCheckOut", "btnCheckOut", "Check Out", "") + "</td>");
            writer.println("</tr>");
            writer.println("</table>");
            writer.println("</form>");
        } catch (Exception e) {
        }
        return SKIP_BODY;
    }
}
